<html>
<head><meta charset="utf-8"><title>Pre-pre RFC: track possible min and max of unknown values · t-compiler · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/index.html">t-compiler</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html">Pre-pre RFC: track possible min and max of unknown values</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="243874598"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243874598" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Bot+ <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243874598">(Jun 25 2021 at 04:30)</a>:</h4>
<p>After getting mad at the compiler for refusing to compile my <code>match x % y</code> (where y is const) without <code>_ =&gt; unreachable!()</code>, I came up with the following idea of tracking the min and max values of (un)signed integers:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">ops</span>::<span class="n">RangeInclusive</span><span class="p">;</span><span class="w"></span>
<span class="k">use</span><span class="w"> </span><span class="n">core</span>::<span class="n">cmp</span>::<span class="n">max</span><span class="p">;</span><span class="w"></span>

<span class="sd">/// Input</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">testA</span><span class="p">(</span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">char</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'x'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'y'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">2</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'z'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">unreachable!</span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// Inject bunch of const stuff to "Range Shadow" signed and unsigned integers</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">_testA</span><span class="p">(</span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">char</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_x0</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="kt">usize</span>::<span class="n">MIN</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">t0</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="n">t0</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_y0</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="w"> </span><span class="n">t0</span><span class="p">.</span><span class="n">saturating_sub</span><span class="p">(</span><span class="mi">1</span><span class="p">));</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_match_arms</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="mi">0_</span><span class="k">usize</span><span class="p">,</span><span class="w"> </span><span class="mi">2_</span><span class="k">usize</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">_is_exhaustive_match</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">_y0</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">_match_arms</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'x'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'y'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">2</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'z'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="n">_</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="fm">unreachable!</span><span class="p">(),</span><span class="w"> </span><span class="c1">// This is provably unreachable</span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// After:</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">__testA</span><span class="p">(</span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">char</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">%</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="mi">0</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'x'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">1</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'y'</span><span class="p">,</span><span class="w"></span>
<span class="w">        </span><span class="mi">2</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="sc">'z'</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="c1">// Input</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">testB</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="kd">let</span><span class="w"> </span><span class="n">y</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">///  Inject bunch of const stuff to "Range Shadow" signed and unsigned integers</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">_testB</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_x0</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="kt">usize</span>::<span class="n">MIN</span><span class="p">,</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="p">);</span><span class="w"></span>

<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">t0</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">3</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_t0</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="n">t0</span><span class="p">,</span><span class="w"> </span><span class="n">t0</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_x1</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">_contains</span><span class="p">(</span><span class="o">*</span><span class="n">_x0</span><span class="p">.</span><span class="n">start</span><span class="p">(),</span><span class="o">*</span><span class="n">_x0</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="o">*</span><span class="n">_t0</span><span class="p">.</span><span class="n">start</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="o">*</span><span class="n">_t0</span><span class="p">.</span><span class="n">start</span><span class="p">(),</span><span class="w"> </span><span class="o">*</span><span class="n">_x0</span><span class="p">.</span><span class="n">end</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">_x0</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">_t0</span><span class="p">.</span><span class="n">start</span><span class="p">()</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">_x0</span><span class="p">.</span><span class="n">start</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="n">t0</span><span class="p">,</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">t1</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_t1</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span><span class="w"> </span><span class="n">t1</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_x2</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w">  </span><span class="o">=</span><span class="w"> </span><span class="k">if</span><span class="w"> </span><span class="n">_contains</span><span class="p">(</span><span class="o">*</span><span class="n">_x1</span><span class="p">.</span><span class="n">start</span><span class="p">(),</span><span class="o">*</span><span class="n">_x1</span><span class="p">.</span><span class="n">end</span><span class="p">(),</span><span class="o">*</span><span class="n">_t1</span><span class="p">.</span><span class="n">start</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="o">*</span><span class="n">_t1</span><span class="p">.</span><span class="n">start</span><span class="p">(),</span><span class="w"> </span><span class="o">*</span><span class="n">_x1</span><span class="p">.</span><span class="n">end</span><span class="p">())</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">_x1</span><span class="w"></span>
<span class="w">    </span><span class="p">};</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">_t1</span><span class="p">.</span><span class="n">start</span><span class="p">()</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">_x1</span><span class="p">.</span><span class="n">start</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="n">t1</span><span class="p">,</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">y</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">_y0</span>: <span class="nc">RangeInclusive</span><span class="o">&lt;</span><span class="kt">usize</span><span class="o">&gt;</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">RangeInclusive</span>::<span class="n">new</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="w"> </span><span class="n">y</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="c1">// If the smallest possible value of x multiplied by the smallest possible value of y overflows</span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="kd">let</span><span class="w"> </span><span class="nb">None</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="p">(</span><span class="n">_x1</span><span class="p">.</span><span class="n">start</span><span class="p">()).</span><span class="n">checked_mul</span><span class="p">(</span><span class="o">*</span><span class="n">_y0</span><span class="p">.</span><span class="n">start</span><span class="p">())</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"attempt to multiply with overflow"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="sd">/// RangeInclusive::contains is not const yet.</span>
<span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">_contains</span><span class="p">(</span><span class="n">start</span>: <span class="kt">usize</span><span class="p">,</span><span class="n">end</span>: <span class="kt">usize</span><span class="p">,</span><span class="n">v</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">&lt;</span><span class="w"> </span><span class="n">start</span><span class="w"> </span><span class="o">||</span><span class="w"> </span><span class="n">v</span><span class="w"> </span><span class="o">&gt;</span><span class="w"> </span><span class="n">end</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kc">false</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"> </span><span class="k">else</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="kc">true</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="sd">/// After:</span>
<span class="k">pub</span><span class="w"> </span><span class="k">fn</span> <span class="nf">__testB</span><span class="p">(</span><span class="k">mut</span><span class="w"> </span><span class="n">x</span>: <span class="kt">usize</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">usize</span> <span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">max</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="n">x</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="n">y</span>: <span class="kt">usize</span> <span class="o">=</span><span class="w"> </span><span class="kt">usize</span>::<span class="n">MAX</span><span class="w"> </span><span class="o">/</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="fm">panic!</span><span class="p">(</span><span class="s">"attempt to multiply with overflow"</span><span class="p">);</span><span class="w"> </span><span class="c1">// Turn into compile time error</span>
<span class="w">    </span><span class="n">x</span><span class="w"> </span><span class="o">*</span><span class="w"> </span><span class="n">y</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>So "Range Shadowing" could be useful for extra compile time errors, dead code elimination, and match exhaustiveness checks.</p>
<p>Thoughts?</p>



<a name="243899956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243899956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243899956">(Jun 25 2021 at 10:48)</a>:</h4>
<p>I have thought about things like ranged integers before. So instead of adding compiler magic for match and integer ops, we could add ranged integers (like <code>struct I32&lt;const RANGE: Range&lt;i32&gt;&gt;(i32);</code>, but with compiler magic for reducing the valid values for the inner i32), which match knows about and will properly use with match exhaustiveness. Then you could create "const types" like <code>struct ConstI32&lt;const X: i32&gt;;</code> and implement <code>Mod&lt;ConstI32&lt;X&gt;&gt; for i32 { Output = I32&lt;{0..X}&gt;}</code> and thus use <code>some_i32 % ConstI32::&lt;4&gt;</code> to obtain an <code>I32&lt;{0..4}&gt;</code>, and pattern matching could then exhaustively match on that.</p>



<a name="243900101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243900101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243900101">(Jun 25 2021 at 10:50)</a>:</h4>
<p>we already have the compiler magic for reducing the valid values for an inner integer, but it's forever unstable and doesn't support const generics yet. It is the feature that gives us NonNull. I would like to change that to use const generics, and then try to implement something like the above I32. It's a longer road to the feature you want, but I think it requires less compiler magic, as most of the work can be done in libraries (or at least in libstd), which reduces the dangers of mistakes, as the logic is simpler to understand than compiler code</p>



<a name="243955103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243955103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243955103">(Jun 25 2021 at 18:47)</a>:</h4>
<p>+1, I'm much rather see this done as types than as typestate.</p>
<p>Relatedly, I'd love to see someone start looking at how we could do custom literals using const generics, and how close the current state of things is.  I'd love it if <code>foo(2)</code> "just worked" even if <code>foo</code> takes a <code>NonZeroU8</code>.</p>



<a name="243957938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243957938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243957938">(Jun 25 2021 at 19:12)</a>:</h4>
<p>to your second part I responded in <a href="#narrow/stream/213817-t-lang/topic/custom.20literals.20using.20const.20generics">https://rust-lang.zulipchat.com/#narrow/stream/213817-t-lang/topic/custom.20literals.20using.20const.20generics</a></p>



<a name="243957978"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/131828-t-compiler/topic/Pre-pre%20RFC%3A%20track%20possible%20min%20and%20max%20of%20unknown%20values/near/243957978" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/131828-t-compiler/topic/Pre-pre.20RFC.3A.20track.20possible.20min.20and.20max.20of.20unknown.20values.html#243957978">(Jun 25 2021 at 19:12)</a>:</h4>
<p>we should probably move this thread to the lang stream, too?</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>